home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / ddj0492.zip / DFLT11.ZIP / MSGBOX.C < prev    next >
Text File  |  1992-01-09  |  5KB  |  207 lines

  1. /* ------------------ msgbox.c ------------------ */
  2.  
  3. #include "dflat.h"
  4.  
  5. extern DBOX MsgBox;
  6. extern DBOX InputBoxDB;
  7. WINDOW CancelWnd;
  8.  
  9. static int ReturnValue;
  10.  
  11. int MessageBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  12. {
  13.     switch (msg)    {
  14.         case CREATE_WINDOW:
  15.             GetClass(wnd) = MESSAGEBOX;
  16.             ClearAttribute(wnd, CONTROLBOX);
  17.             break;
  18.         case KEYBOARD:
  19.             if (p1 == '\r' || p1 == ESC)
  20.                 ReturnValue = (int)p1;
  21.             break;
  22.         default:
  23.             break;
  24.     }
  25.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  26. }
  27.  
  28. int YesNoBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  29. {
  30.     switch (msg)    {
  31.         case CREATE_WINDOW:
  32.             GetClass(wnd) = MESSAGEBOX;
  33.             ClearAttribute(wnd, CONTROLBOX);
  34.             break;
  35.         case KEYBOARD:    {
  36.             int c = tolower((int)p1);
  37.             if (c == 'y')
  38.                 SendMessage(wnd, COMMAND, ID_OK, 0);
  39.             else if (c == 'n')
  40.                 SendMessage(wnd, COMMAND, ID_CANCEL, 0);
  41.             break;
  42.         }
  43.         default:
  44.             break;
  45.     }
  46.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  47. }
  48.  
  49. int ErrorBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  50. {
  51.     switch (msg)    {
  52.         case CREATE_WINDOW:
  53.             GetClass(wnd) = ERRORBOX;
  54.             break;
  55.         case KEYBOARD:
  56.             if (p1 == '\r' || p1 == ESC)
  57.                 ReturnValue = (int)p1;
  58.             break;
  59.         default:
  60.             break;
  61.     }
  62.     return BaseWndProc(ERRORBOX, wnd, msg, p1, p2);
  63. }
  64.  
  65. int CancelBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  66. {
  67.     switch (msg)    {
  68.         case CREATE_WINDOW:
  69.             CancelWnd = wnd;
  70.             SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  71.             SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  72.             break;
  73.         case COMMAND:
  74.             if ((int) p1 == ID_CANCEL && (int) p2 == 0)
  75.                 SendMessage(GetParent(wnd), msg, p1, p2);
  76.             return TRUE;
  77.         case CLOSE_WINDOW:
  78.             CancelWnd = NULL;
  79.             SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  80.             SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  81.             p1 = TRUE;
  82.             break;
  83.         default:
  84.             break;
  85.     }
  86.     return BaseWndProc(MESSAGEBOX, wnd, msg, p1, p2);
  87. }
  88.  
  89. void CloseCancelBox(void)
  90. {
  91.     if (CancelWnd != NULL)
  92.         SendMessage(CancelWnd, CLOSE_WINDOW, 0, 0);
  93. }
  94.  
  95. static char *InputText;
  96. static int TextLength;
  97.  
  98. int InputBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  99. {
  100.     int rtn;
  101.     switch (msg)    {
  102.         case CREATE_WINDOW:
  103.             rtn = DefaultWndProc(wnd, msg, p1, p2);
  104.             SendMessage(ControlWindow(&InputBoxDB, ID_INPUTTEXT),
  105.                         SETTEXTLENGTH, TextLength, 0);
  106.             return rtn;
  107.         case COMMAND:
  108.             if ((int) p1 == ID_OK && (int) p2 == 0)
  109.                 GetItemText(wnd, ID_INPUTTEXT,
  110.                             InputText, TextLength);
  111.             break;
  112.         default:
  113.             break;
  114.     }
  115.     return DefaultWndProc(wnd, msg, p1, p2);
  116. }
  117.  
  118. BOOL InputBox(WINDOW wnd, char *ttl, char *msg, char *text, int len)
  119. {
  120.     InputText = text;
  121.     TextLength = len;
  122.     InputBoxDB.dwnd.title = ttl;
  123.     InputBoxDB.dwnd.w = 4 + 
  124.         max(20, max(len, max(strlen(ttl), strlen(msg))));
  125.     InputBoxDB.ctl[1].dwnd.x = (InputBoxDB.dwnd.w-2-len)/2;
  126.     InputBoxDB.ctl[0].dwnd.w = strlen(msg);
  127.     InputBoxDB.ctl[0].itext = msg;
  128.     InputBoxDB.ctl[1].dwnd.w = len;
  129.     InputBoxDB.ctl[2].dwnd.x = (InputBoxDB.dwnd.w - 20) / 2;
  130.     InputBoxDB.ctl[3].dwnd.x = InputBoxDB.ctl[2].dwnd.x + 10;
  131.     InputBoxDB.ctl[2].isetting = ON;
  132.     InputBoxDB.ctl[3].isetting = ON;
  133.     return DialogBox(wnd, &InputBoxDB, TRUE, InputBoxProc);
  134. }
  135.  
  136. BOOL GenericMessage(WINDOW wnd, char *ttl, char *msg, int buttonct,
  137.     int (*wndproc)(struct window *, enum messages, PARAM, PARAM),
  138.     char *b1, char *b2, int c1, int c2, int isModal)
  139. {
  140.     BOOL rtn;
  141.     MsgBox.dwnd.title = ttl;
  142.     MsgBox.ctl[0].dwnd.h = MsgHeight(msg);
  143.     MsgBox.ctl[0].dwnd.w = max(max(MsgWidth(msg),
  144.             buttonct*8 + buttonct + 2), strlen(ttl)+2);
  145.     MsgBox.dwnd.h = MsgBox.ctl[0].dwnd.h+6;
  146.     MsgBox.dwnd.w = MsgBox.ctl[0].dwnd.w+4;
  147.     if (buttonct == 1)
  148.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 10) / 2;
  149.     else    {
  150.         MsgBox.ctl[1].dwnd.x = (MsgBox.dwnd.w - 20) / 2;
  151.         MsgBox.ctl[2].dwnd.x = MsgBox.ctl[1].dwnd.x + 10;
  152.         MsgBox.ctl[2].class = BUTTON;
  153.     }
  154.     MsgBox.ctl[1].dwnd.y = MsgBox.dwnd.h - 4;
  155.     MsgBox.ctl[2].dwnd.y = MsgBox.dwnd.h - 4;
  156.     MsgBox.ctl[0].itext = msg;
  157.     MsgBox.ctl[1].itext = b1;
  158.     MsgBox.ctl[2].itext = b2;
  159.     MsgBox.ctl[1].command = c1;
  160.     MsgBox.ctl[2].command = c2;
  161.     MsgBox.ctl[1].isetting = ON;
  162.     MsgBox.ctl[2].isetting = ON;
  163.     rtn = DialogBox(wnd, &MsgBox, isModal, wndproc);
  164.     MsgBox.ctl[2].class = 0;
  165.     return rtn;
  166. }
  167.  
  168. WINDOW MomentaryMessage(char *msg)
  169. {
  170.     WINDOW wnd = CreateWindow(
  171.                     TEXTBOX,
  172.                     NULL,
  173.                     -1,-1,MsgHeight(msg)+2,MsgWidth(msg)+2,
  174.                     NULL,NULL,NULL,
  175.                     HASBORDER | SHADOW | SAVESELF);
  176.     SendMessage(wnd, SETTEXT, (PARAM) msg, 0);
  177.     if (cfg.mono == 0)    {
  178.         WindowClientColor(wnd, WHITE, GREEN);
  179.         WindowFrameColor(wnd, WHITE, GREEN);
  180.     }
  181.     SendMessage(wnd, SHOW_WINDOW, 0, 0);
  182.     return wnd;
  183. }
  184.  
  185. int MsgHeight(char *msg)
  186. {
  187.     int h = 1;
  188.     while ((msg = strchr(msg, '\n')) != NULL)    {
  189.         h++;
  190.         msg++;
  191.     }
  192.     return min(h, SCREENHEIGHT-10);
  193. }
  194.  
  195. int MsgWidth(char *msg)
  196. {
  197.     int w = 0;
  198.     char *cp = msg;
  199.     while ((cp = strchr(msg, '\n')) != NULL)    {
  200.         w = max(w, (int) (cp-msg));
  201.         msg = cp+1;
  202.     }
  203.     return min(max(strlen(msg),w), SCREENWIDTH-10);
  204. }
  205.  
  206.  
  207.